package cn.meomis.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import cn.meomis.common.entity.Pagination;
import cn.meomis.common.util.DataTableParam;
import cn.meomis.model.SysLog;
import cn.meomis.service.SysLogService;

//@Scope("prototype") 
@Controller
//@Namespace("/")
@ParentPackage("json-default")
//@Result(name="success", type="json")
public class SysLogAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private SimpleDateFormat dateformat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	
	// @Resource
	@Autowired
	private SysLogService sysLogService;
	
	

	
	@Action(value = "/SysLogAction", results = { @Result(name = SUCCESS, location = "/manager/modules/syslog/sysLog.jsp")})
	@Override
	public String execute() {
		return SUCCESS;
	}
	
	
	@SuppressWarnings({ "unchecked", "unused" })
	@Action(value="/sysLogListAction")
	public void listDestination() {
		DataTableParam params = null;
		try {
			params = getParam();
		} catch (JSONException e1) {
			e1.printStackTrace();
		}
		HashMap<String, String> paramMap = convertToMap(params.getVoList());	
		String sEcho = paramMap.get("sEcho");
		Long iTotalRecords; // total number of records (unfiltered)
		Long iTotalDisplayRecords; //value will be set when code filters companies by keyword
		JSONArray data = new JSONArray(); //data that will be shown in the table

		int pageNumber = (Integer.parseInt(paramMap.get("iDisplayStart")) / Integer.parseInt(paramMap.get("iDisplayLength"))) + 1;

		StringBuffer hql =new StringBuffer( "from SysLog demo ");
		if (params.getIsQuery().equalsIgnoreCase("true")) {
			hql.append(" where 1 = 1 ");
			String content = paramMap.get("content").trim();
//			String des = paramMap.get("des").trim();
			if (!"".equalsIgnoreCase(content)) {
				hql.append(" and demo.content like '%"+content+"%' ");
			}
			
/*			if (!"".equalsIgnoreCase(des)) {
				hql.append(" and demo.des like '%"+des+"%' ");
			}*/
		}
		
		String sCol = paramMap.get("iSortCol_0");
		String sortingColName =  paramMap.get("mDataProp_"+sCol);
		//asc or desc
	    String sdir = paramMap.get("sSortDir_0");
	   
	    if("0".equals(sortingColName)){
//	    	sortingColName = "dest.destination";
	    }else if("1".equals(sortingColName)){
	    	sortingColName = "content";
	    }else if("2".equals(sortingColName)){
	    	sortingColName = "createDate";
	    }
	    
    	if ("desc".equalsIgnoreCase(sdir)) {
	    	hql.append(" order by "+sortingColName +" desc");
	    }else{
	    	hql.append(" order by "+sortingColName );
	    }
	    

		Object[] values = null;
		Pagination pageMqDestination = sysLogService.pageQueryByHql(hql.toString(), values, hql.toString(), pageNumber, Integer.parseInt(paramMap.get("iDisplayLength")));
		iTotalRecords = pageMqDestination.getTotalCount();
		List<SysLog> companies = (List<SysLog>) pageMqDestination.getResult();
//		List<Value> companies = new LinkedList<Demo>();
//		for(Demo c : apps){
//			companies.add(c);
//		}
		
		iTotalDisplayRecords = pageMqDestination.getTotalCount();

	
		try {
			JSONObject jsonResponse = new JSONObject();			
			jsonResponse.put("sEcho", sEcho);
			jsonResponse.put("iTotalRecords", iTotalRecords);
			jsonResponse.put("iTotalDisplayRecords", iTotalDisplayRecords);
			int i = 1;
			for(SysLog c : companies){
				JSONArray row = new JSONArray();
				row.put(i++);
				row.put(c.getContent());
//				row.put(c.getDes());
				row.put(dateformat.format(c.getCreateDate()));
				row.put(c.getId());
				data.put(row);
			}
			jsonResponse.put("resultList", data);
			httpServletResponse.setContentType("application/Json;charset=utf-8");
			httpServletResponse.getWriter().print(jsonResponse.toString());
			
		} catch (Exception e) {
			e.printStackTrace();
			httpServletResponse.setContentType("text/html");
			try {
				httpServletResponse.getWriter().print(e.getMessage());
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

}
